package dk.kapetanovic.jaft.transaction;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.util.List;

import dk.kapetanovic.jaft.action.Action;
import dk.kapetanovic.jaft.action.DeleteAction;
import dk.kapetanovic.jaft.action.OpenFileOutputStreamAction;
import dk.kapetanovic.jaft.action.OpenFileWriterAction;
import dk.kapetanovic.jaft.action.RenameAction;
import dk.kapetanovic.jaft.exception.InconsistentStateException;
import dk.kapetanovic.jaft.exception.TransactionException;


public class TransactionHandler implements Transaction {

	private Transaction transaction; 

	public TransactionHandler(Transaction transaction) {
		this.transaction = transaction;
	}
	
	public void commit() throws InconsistentStateException,
			TransactionException {
		transaction.commit();
	}
	
	public void rollback() throws InconsistentStateException,
			TransactionException {
		transaction.rollback();
	}
	
	public void rename(File src, File dest) throws InconsistentStateException, 
			TransactionException {
		Action rename = new RenameAction(src, dest, transaction);
		transaction.registerAction(rename);
	}
	
	public void rename(String src, String dest) throws InconsistentStateException, 
			TransactionException {
		rename(new File(src), new File(dest));
	}
	
	public void delete(File file) throws InconsistentStateException, 
			TransactionException {
		Action delete = new DeleteAction(file, transaction);
		transaction.registerAction(delete);
	}
	
	public void delete(String file) throws InconsistentStateException, 
			TransactionException {
		delete(new File(file));
	}
	
	public FileOutputStream openFileOutputStream(File file, boolean append) throws 
			InconsistentStateException, TransactionException {
		Action open = new OpenFileOutputStreamAction(file, append, transaction);
		return (FileOutputStream) transaction.registerAction(open);
	}
	
	public FileOutputStream openFileOutputStream(String file, boolean append) throws 
			InconsistentStateException, TransactionException {
		return openFileOutputStream(new File(file), append);
	}
	
	public FileWriter openFileWriter(File file, boolean append) throws 
			InconsistentStateException, TransactionException {
		Action wrt = new OpenFileWriterAction(file, append, transaction);
		return (FileWriter) transaction.registerAction(wrt);
	}
	
	public FileWriter openFileWriter(String file, boolean append) throws 
			InconsistentStateException, TransactionException {
		return openFileWriter(new File(file), append);
	}

	@Override
	public boolean checkError() {
		return transaction.checkError();
	}

	@Override
	public void end() throws InconsistentStateException, TransactionException {
		transaction.end();
	}

	@Override
	public List<Exception> getCleanupExceptions() {
		return transaction.getCleanupExceptions();
	}

	@Override
	public List<Exception> getCloseExceptions() {
		return transaction.getCloseExceptions();
	}

	@Override
	public TransactionManager getTransactionManager() {
		return transaction.getTransactionManager();
	}

	@Override
	public boolean isActive() {
		return transaction.isActive();
	}

	@Override
	public boolean isCommited() {
		return transaction.isCommited();
	}

	@Override
	public Object registerAction(Action action)
			throws InconsistentStateException, TransactionException {
		return transaction.registerAction(action);
	}

	@Override
	public void setTransactionManager(TransactionManager manager) {
		transaction.setTransactionManager(manager);
	}

	@Override
	public void shouldCommit(boolean commit) {
		transaction.shouldCommit(commit);
	}
}
